<template>
	<view class="wrap">
		<view class="search" >
			<u-search placeholder="请输入产品名称" v-model="keyword" @search="search" @custom="search"></u-search>
		</view>
		<u-empty text="没有找到搜索结果" mode="search" margin-top="200" :show="isEmpty"></u-empty>
		<custom-waterfall :searchList="searchList" ref="customWaterfall" v-if="isWaterShow"></custom-waterfall>
		<u-divider bg-color="#f0f0f0" color="red" border-color="red" height="80">猜您想看</u-divider>
		<custom-waterfall :searchList="hotSell" ref="customWaterfall"></custom-waterfall>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				keyword:'',
				loadStatus: 'loadmore',
				isEmpty:false,
				isWaterShow:true
			}
		},
		onLoad() {
			// this.addRandomData();
			this.$store.dispatch('getHotSell');
		},
		onReachBottom() {
			// this.loadStatus = 'loading';
			// 模拟数据加载
			// setTimeout(() => {
			// 	this.addRandomData();
			// 	this.loadStatus = 'loadmore';
			// }, 1000)
		},
		computed:{
			...mapState({
				searchList:(state)=>state.category.searchList || [],
				hotSell:(state)=>state.category.hotSell || []
			})
		},
		methods: {
			async search(){
				if(this.keyword==''){
					this.isEmpty = true;
					return;
				} 
				this.isWaterShow = false;
				await this.$store.dispatch('searchCategory',this.keyword);
				if(this.searchList.length==0){
					this.isEmpty = true;
					return;
				}else{
					this.isEmpty = false;
				}
				// 强制视图更新
				this.isWaterShow = true;
			},
			addRandomData() {
				// for(let i = 0; i < this.searchList.length; i++) {
					// let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					// let item = JSON.parse(JSON.stringify(this.list[index]))
					// item.id = this.$u.guid();
					// this.flowList.push(item);
					// let item = this.searchList[i];
					// this.flowList.push(item);
				// }
				this.flowList = this.searchList;
			},
			remove(id) {
				this.$refs.uWaterfall.remove(id);
			},
			clear() {
				this.$refs.uWaterfall.clear();
			}
		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		/* background-color: rgb(240, 240, 240); */
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.search {
		background-color: #FFFFFF;
		height: 115rpx;
		padding: 20rpx 10rpx 0rpx 10rpx;
	}
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}
	
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}
	
	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}
	
	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}
	
	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}
	
	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>